package com.yituo.mjworld.core.utils;

import org.junit.jupiter.api.Test;
import org.springframework.util.StringUtils;

import java.io.File;
import java.security.MessageDigest;
import java.util.UUID;

/**
 * 杂七杂八的工具类
 *
 * @author xq
 */
public class OtherTool {


  /**
   * 删除文件
   *
   * @param filePath
   * @return
   */
  public static boolean fileIsExist(String filePath) {
    if (StringUtils.isEmpty(filePath)) {
      return false;
    }
    File file = new File(filePath);
    return file.exists();
  }

  /**
   * 删除文件
   *
   * @param filePath
   * @return
   */
  public static boolean deleteFile(String filePath) {
    if (StringUtils.isEmpty(filePath)) {
      return false;
    }
    File file = new File(filePath);
    return file.delete();
  }

  /**
   * 获取文件的扩展名
   *
   * @return
   */
  public static String getFileExtName(String fileAllName) {
    if (StringUtils.isEmpty(fileAllName)) {
      return null;
    }
    return fileAllName.substring(fileAllName.lastIndexOf(".") + 1, fileAllName.length());
  }

  public static boolean contain(String[] arr, String value) {
    if (value == null) {
      return false;
    }
    if (arr != null && arr.length > 0) {
      for (String item : arr) {
        if (item.equals(value)) {
          return true;
        }
      }
    }
    return false;
  }

  /***
   * 获取SHA1地址
   * 文档地址:
   * http://blog.csdn.net/u012105931/article/details/51088312
   * @param str
   * @return
   */
  public static String getSha1(String str) {
    if (str == null || str.length() == 0) {
      return null;
    }
    char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'a', 'b', 'c', 'd', 'e', 'f'};
    try {
      MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
      mdTemp.update(str.getBytes("UTF-8"));

      byte[] md = mdTemp.digest();
      int j = md.length;
      char buf[] = new char[j * 2];
      int k = 0;
      for (int i = 0; i < j; i++) {
        byte byte0 = md[i];
        buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
        buf[k++] = hexDigits[byte0 & 0xf];
      }
      return new String(buf);
    } catch (Exception e) {
      // TODO: handle exception
      return null;
    }
  }

  /**
   * MD5解析失败时,将返回一个32位长的UUID
   *
   * @param str
   * @return
   */
  public static String getMd5OrUUID(String str) {
    String result = "";
    MessageDigest md5 = null;
    try {
      md5 = MessageDigest.getInstance("MD5");
      md5.update((str).getBytes("UTF-8"));
    } catch (Exception e) {
      e.printStackTrace();
      return UUID.randomUUID().toString().replace("-", "");
    }
    byte[] b = md5.digest();
    int i;
    StringBuffer buf = new StringBuffer("");
    for (int offset = 0; offset < b.length; offset++) {
      i = b[offset];
      if (i < 0) {
        i += 256;
      }
      if (i < 16) {
        buf.append("0");
      }
      buf.append(Integer.toHexString(i));
    }
    result = buf.toString();
    return result;
  }

  /**
   * 获取32位长UUID
   *
   * @return
   */
  public static String getUUID() {
    return UUID.randomUUID().toString().replace("-", "");
  }

  /**
   * 获取随机的6位长的验证码
   *
   * @return
   */
  public static int getCode() {
    return (int) ((Math.random() * 9 + 1) * 100000);
  }

  @Test
  public void test() {
    System.out.println(OtherTool.getFileExtName("http://111.jpg"));
  }

}
